<template>
  <div>
    <!-- 筛选组件 -->
    <el-form ref="searchForm" :inline="true" :model="searchMap" class="demo-form-inline">
      <!-- 重置会看 el-form-item 组件元素的prop上是否指定了字段名 -->
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="searchMap.code" placeholder="商品编号"></el-input>
      </el-form-item>
      <el-form-item prop="supplierName">
        <el-input
          @click.native="dialogSupplierVisible = true"
          v-model="searchMap.supplierName"
          placeholder="选择供应商"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="fetchData"
          style="margin-left:20px"
          class="el-icon-search"
        >&nbsp;查询</el-button>
        <el-button type="success" @click="handleAdd" class="el-icon-plus">&nbsp;新增</el-button>
        <el-button @click="$refs['searchForm'].resetFields();">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表单组件 -->
    <el-table :data="list" style="width: 100%" border>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="name" label="商品名称" width="150px"></el-table-column>
      <el-table-column prop="code" label="商品编号" width="120px"></el-table-column>
      <el-table-column prop="spec" label="商品规格" width="110px"></el-table-column>
      <el-table-column prop="retailPrice" label="零售价" width="110px"></el-table-column>
      <el-table-column prop="purchasePrice" label="进货价" width="110px"></el-table-column>
      <el-table-column prop="storageNum" label="库存数量" width="110px"></el-table-column>
      <el-table-column prop="supplierName" label="供应商"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div class="block">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 选择供应商对话框 -->
    <el-dialog title="选择供应商" :visible.sync="dialogSupplierVisible" width="500px">
      <supplier :isDialog="true" @option-supplier="optionSupplier"></supplier>
    </el-dialog>

    <!-- 编辑商品 -->
    <el-dialog title="编辑商品" :visible.sync="dialogFormVisible" width="500px">
      <el-form
        :rules="rules"
        :model="pojo"
        ref="pojoForm"
        label-width="100px"
        label-position="right"
        style="width:400px"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="pojo.name"></el-input>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="pojo.code"></el-input>
        </el-form-item>
        <el-form-item label="商品规格" prop="spec">
          <el-input v-model="pojo.spec"></el-input>
        </el-form-item>
        <el-form-item label="零售价" prop="retailPrice">
          <el-input v-model="pojo.retailPrice"></el-input>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input v-model="pojo.purchasePrice"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="storageNum">
          <el-input v-model="pojo.storageNum"></el-input>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input
            @click.native="editOptionSupplier"
            v-model="pojo.supplierName"
            placeholder="选择供应商"
            readonly
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!-- <el-button type="primary" @click="addData('pojoForm')">确 定</el-button> -->
        <el-button
          type="primary"
          @click="pojo.id === null ? addData('pojoForm') : updateData('pojoForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import goodsApi from "@/api/goods";
import Supplier from "@/views/supplier";

export default {
  components: { Supplier },
  data() {
    return {
      list: [],
      currentPage: 1,
      pageSize: 8,
      total: 0,
      isEdit: false, //是否为编辑窗口
      searchMap: {
        name: null,
        code: null,
        supplierName: null
      },
      dialogSupplierVisible: false,
      dialogFormVisible: false,
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
        retailPrice: [
          { required: true, message: "零售价不能为空", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "供应商不能为空", trigger: "change" }
        ]
      },
      pojo: {
        id: null,
        supplierId: null,
        name: "",
        code: "",
        spec: "",
        retailPrice: "",
        purchasePrice: "",
        storageNum: "",
        supplierName: ""
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      goodsApi
        .search(this.currentPage, this.pageSize, this.searchMap)
        .then(response => {
          const resp = response.data;
          this.list = resp.data.rows;
          this.total = resp.data.total;
        });
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    optionSupplier(currentRow) {
      if (this.isEdit) {
        // 是编辑窗口打开的供应商
        this.pojo.supplierName = currentRow.name;
        this.pojo.supplierId = currentRow.id;
      } else {
        // 是搜索区域打开的供应商
        this.searchMap.supplierName = currentRow.name; // 供应商名称
        this.searchMap.supplierId = currentRow.id; // 供应商编号
      }
      this.isEdit = false; // 一定要重新复制为false
      this.dialogSupplierVisible = false; // 关闭窗口
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["pojoForm"].resetFields();
      });
    },
    addData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验通过,提交表单
          goodsApi.add(this.pojo).then(response => {
            const resp = response.data;
            if (resp.flag) {
              // 新增成功,舒心列表数据
              this.fetchData();
              // 关闭窗口
              this.dialogFormVisible = false;
              this.$message({
                message: resp.message,
                type: "success"
              });
            } else {
              this.$message({
                message: resp.message,
                type: "warning"
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    editOptionSupplier() {
      this.isEdit = true;
      this.dialogSupplierVisible = true;
    },
    // 删除会员
    handleDelete(id) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          goodsApi.deleteById(id).then(response => {
            const resp = response.data;
            this.$message({
              message: resp.message,
              type: resp.flag ? "success" : "error"
            });
            if (resp.flag) {
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleEdit(id) {
      this.handleAdd();
      goodsApi.getById(id).then(response => {
        const resp = response.data;
        if (resp.flag) {
          this.pojo = resp.data;
        }
      });
    },
    updateData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验通过,提交表单
          goodsApi.update(this.pojo).then(response => {
            const resp = response.data;
            if (resp.flag) {
              // 新增成功,舒心列表数据
              this.fetchData();
              // 关闭窗口
              this.dialogFormVisible = false;
              this.$message({
                message: resp.message,
                type: "success"
              });
            } else {
              this.$message({
                message: resp.message,
                type: "warning"
              });
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.el-form {
  margin: 20px 0 -6px;
}
.block {
  margin: 20px 0 0 5px;
}
</style>